import 'reflect-metadata'
import { ConnectionOptions, createConnection } from 'typeorm'

import { Task } from '@app/models/task'
import { Block } from '@app/models/block'
import { Receive } from '@app/models/receive'
import { KeyStore } from '@app/models/keystore'
import { Transaction } from '@app/models/transaction'
import * as config from '@config/server.json'

async function openConnect(dbname: string) {
    const options: ConnectionOptions = {
        type: 'sqlite',
        database: config.datadir + `/${dbname}.db`,
        entities: [ Block, Receive, Task, Transaction ],
        logging: false
    }

    const connection = await createConnection(options)
    await connection.synchronize()

    const keystoreOptions: ConnectionOptions = {
        type: 'sqlite',
        name: 'keystore',
        database: config.datadir + '/keystore.db',
        entities: [ KeyStore ],
        logging: false
    }

    const keyStoreConnection = await createConnection(keystoreOptions)
    await keyStoreConnection.synchronize()
}

export { openConnect }
